<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/04/11 20:58:15 $)
// $RCSfile: admin_priorities.php,v $ : $Revision: 1.3 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

if (!$staffauth->isAdmin()) {
	trigger_error("You do not have permission to access $PHP_SELF", E_USER_ERROR);
}

require_once ("./modules/teamwork/functions_misc.php");
require_once ("./includes/functions_html.php");

$template->loadLanguageSection("admin_teamwork");

/**
* ###############################################
* FUNCTION DECLARATIONS
* ###############################################
*/

/**
* Callback for Calendar Priorities
*/
function _calendarPriorityFields($arg)
{
	global $_SWIFT;
	$record = &$arg[0];
	$fields = &$arg[1];

	$record["title"] = '<a href="index.php?_m=teamwork&_a=editpriority&calendarpriorityid='. $record["calendarpriorityid"] .'"><img src="'. $_SWIFT["themepath"] .'icon_teamworkpriority.gif" border="0" align="absmiddle" /> <font color="'. $record["prioritycolorcode"] .'">'.htmlspecialchars($record["title"]).'</font></a>';

	$record["options"] = '<a href="index.php?_m=teamwork&_a=editpriority&calendarpriorityid='. $record["calendarpriorityid"] .'" title="'. $_SWIFT["language"]["edit"] .'"><img src="'. $_SWIFT["themepath"] .'icon_edit.gif" border="0">&nbsp;'. $_SWIFT["language"]["edit"] .'</a>&nbsp;&nbsp;&nbsp;&nbsp;<a onClick="javascript:doConfirm(\''. $_SWIFT["language"]["actionconfirm"] .'\', \'index.php?_m=teamwork&_a=managepriorities&do=delete&calendarpriorityid='. $record["calendarpriorityid"] .'\');" href="#" title="'. $_SWIFT["language"]["delete"] .'"><img src="'. $_SWIFT["themepath"] .'icon_delete.gif" border="0">&nbsp;'. $_SWIFT["language"]["delete"] .'</a>&nbsp;';

	return $record;
}

/**
* Delete Callback
*/
function _maDeleteCalendarPriority()
{
	global $dbCore, $_SWIFT;

	if ($_SWIFT["isdemo"] == true)
	{
		return false;
	}

	if (is_array($_POST["itemid"]))
	{
		deleteCalendarPriority($_POST["itemid"]);
		$_SWIFT["infomessage"] = $_SWIFT["language"]["prioritymuldelconfirm"];
	}
}


/**
* ###############################################
* MANAGE PRIORITIES
* ###############################################
*/
if ($eventaction == "managepriorities")
{
	if ($_GET["do"] == "delete" && !empty($_GET["calendarpriorityid"]) && $_SWIFT["isdemo"] != true)
	{
		$_SWIFT["infomessage"] = $_SWIFT["language"]["prioritymuldelconfirm"];

		deleteCalendarPriority(array($_GET["calendarpriorityid"]));
	}

	if (!empty($_GET["insertpriority"]))
	{
		$_SWIFT["infomessage"] = sprintf($_SWIFT["language"]["priorityinsertconfirm"], htmlspecialchars($_GET["insertpriority"]));
	} else if (!empty($_GET["updatepriority"])) {
		$_SWIFT["infomessage"] = sprintf($_SWIFT["language"]["priorityupdateconfirm"], htmlspecialchars($_GET["updatepriority"]));
	}

	$options["recordsperpage"] = "15";
	$options["sortby"] = "displayorder";
	$options["sortorder"] = "asc";
	$options["massaction"][0]["title"] = $_SWIFT["language"]["delete"];
	$options["massaction"][0]["callback"] = "_maDeleteCalendarPriority";

	$options["idname"] = "calendarpriorityid";
	$options["quicksearch"] = true;

	$options["advancedsearch"][0]["title"] = $_SWIFT["language"]["title"];
	$options["advancedsearch"][0]["query"] = 'SELECT * FROM `'. TABLE_PREFIX .'calendarpriorities` WHERE `title` LIKE \'%$_searchstr%\' $_sortjoin;';

	$options["callback"] = "_calendarPriorityFields";

	$options["toolbar"][0]["title"] = $_SWIFT["language"]["insertpriority"];
	$options["toolbar"][0]["url"] = "index.php?_m=teamwork&_a=insertpriority";

	$fields[0]["name"] = "title";
	$fields[0]["title"] = $_SWIFT["language"]["prioritytitle"];
	$fields[0]["width"] = "";

	$fields[1]["name"] = "displayorder";
	$fields[1]["title"] = $_SWIFT["language"]["displayorder"];
	$fields[1]["width"] = "120";
	$fields[1]["align"] = "center";

	$fields[2]["type"] = "custom";
	$fields[2]["name"] = "options";
	$fields[2]["title"] = $_SWIFT["language"]["options"];
	$fields[2]["width"] = "150";
	$fields[2]["align"] = "center";

	$grid = new Grid();

	$interface->adminHeader($_SWIFT["language"]["teamwork"]." > ".$_SWIFT["language"]["managepriorities"], 1);

	$interface->adminNavBar('<a href="index.php?_m=core&_a=dashboard" title="'.$_SWIFT["language"]["dashboard"].'">'.$_SWIFT["language"]["dashboard"].'</a> &raquo; <a href="index.php?_m=teamwork&_a=managestatus" title="'.$_SWIFT["language"]["teamwork"].'">'.$_SWIFT["language"]["teamwork"].'</a> &raquo; <a href="index.php?_m=teamwork&_a=managepriorities" title="'.$_SWIFT["language"]["managepriorities"].'">'.$_SWIFT["language"]["managepriorities"].'</a>', $_SWIFT["language"]["desc_priorities"] , 15);


	$grid->start("teampriority", $_SWIFT["language"]["prioritylist"], 'SELECT * FROM `'. TABLE_PREFIX .'calendarpriorities` $_sortjoin;', 'SELECT COUNT(*) AS totalitems FROM `'. TABLE_PREFIX .'calendarpriorities`;', 'SELECT * FROM `'. TABLE_PREFIX .'calendarpriorities` WHERE `title` LIKE \'%$_searchstr%\' $_sortjoin;', $fields, $options);

	printInfoBox($_SWIFT["infomessage"]);
	printErrorBox($_SWIFT["errormessage"]);

	$grid->display("teampriority");
	
	$interface->adminFooter();



/**
* ###############################################
* INSERT PRIORITY
* ###############################################
*/
} else if ($eventaction == "insertpriority") {
	if ($_POST["step"] == "1")
	{
		if (trim($_POST["title"]) == "" || trim($_POST["displayorder"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else if ($_SWIFT["isdemo"] == true) {
			$errormessage = $_SWIFT["language"]["demomode"];
		} else {
			insertCalendarPriority(PRIORITY_TASK, $_POST["title"], $_POST["prioritycolorcode"], $_POST["displayorder"]);

			printRedirect(sprintf($_SWIFT["language"]["priorityinsertconfirm"], htmlspecialchars($_POST["title"])), "index.php?_m=teamwork&_a=managepriorities&insertpriority=".urlencode($_POST["title"]));
			exit;
		}
	}

	$interface->adminHeader($_SWIFT["language"]["teamwork"]." > ".$_SWIFT["language"]["insertpriority"], 1);

	$interface->adminNavBar('<a href="index.php?_m=core&_a=dashboard" title="'.$_SWIFT["language"]["dashboard"].'">'.$_SWIFT["language"]["dashboard"].'</a> &raquo; <a href="index.php?_m=teamwork&_a=managestatus" title="'.$_SWIFT["language"]["teamwork"].'">'.$_SWIFT["language"]["teamwork"].'</a> &raquo; <a href="index.php?_m=teamwork&_a=managepriorities" title="'.$_SWIFT["language"]["managepriorities"].'">'.$_SWIFT["language"]["managepriorities"].'</a> &raquo; <a href="index.php?_m=teamwork&_a=insertpriority" title="'.$_SWIFT["language"]["insertpriority"].'">'.$_SWIFT["language"]["insertpriority"].'</a>', $_SWIFT["language"]["desc_managepriorities"] , 15);

	printInfoBox($_SWIFT["infomessage"]);
	printErrorBox($_SWIFT["errormessage"]);

	renderCalendarPriorityForm(INSERT);

	$template->assign("backurl", "index.php?_m=teamwork&_a=managepriorities");
	$interface->adminFooter();



/**
* ###############################################
* EDIT PRIORITY
* ###############################################
*/
} else if ($eventaction == "editpriority") {
	$_calendarpriority = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."calendarpriorities` WHERE `calendarpriorityid` = '". intval($_REQUEST["calendarpriorityid"]) ."';");
	if (empty($_calendarpriority["calendarpriorityid"]))
	{
		trigger_error($_SWIFT["language"]["invalidpriority"], E_USER_ERROR);
	}

	if ($_POST["step"] == "1")
	{
		if (trim($_POST["title"]) == "" || trim($_POST["displayorder"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else if ($_SWIFT["isdemo"] == true) {
			$errormessage = $_SWIFT["language"]["demomode"];
		} else {
			updateCalendarPriority($_calendarpriority["calendarpriorityid"], PRIORITY_TASK, $_POST["title"], $_POST["prioritycolorcode"], $_POST["displayorder"]);

			printRedirect(sprintf($_SWIFT["language"]["priorityupdateconfirm"], htmlspecialchars($_POST["title"])), "index.php?_m=teamwork&_a=managepriorities&updatepriority=".urlencode($_POST["title"]));
			exit;
		}
	}

	$_POST = array_merge($_POST, $_calendarpriority);

	$interface->adminHeader($_SWIFT["language"]["teamwork"]." > ".$_SWIFT["language"]["editpriority"], 1);

	$interface->adminNavBar('<a href="index.php?_m=core&_a=dashboard" title="'.$_SWIFT["language"]["dashboard"].'">'.$_SWIFT["language"]["dashboard"].'</a> &raquo; <a href="index.php?_m=teamwork&_a=managestatus" title="'.$_SWIFT["language"]["teamwork"].'">'.$_SWIFT["language"]["teamwork"].'</a> &raquo; <a href="index.php?_m=teamwork&_a=managepriorities" title="'.$_SWIFT["language"]["managepriorities"].'">'.$_SWIFT["language"]["managepriorities"].'</a> &raquo; <a href="index.php?_m=teamwork&_a=editpriority&calendarpriorityid='. intval($_calendarpriority["calendarpriorityid"]) .'" title="'.$_SWIFT["language"]["editpriority"].'">'.$_SWIFT["language"]["editpriority"].'</a>', $_SWIFT["language"]["desc_managepriorities"] , 15);

	printInfoBox($_SWIFT["infomessage"]);
	printErrorBox($_SWIFT["errormessage"]);

	renderCalendarPriorityForm(EDIT);

	$template->assign("backurl", "index.php?_m=teamwork&_a=managepriorities");
	$interface->adminFooter();
	
}



?>